package com.sale.salesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sale.salesystem.entity.SysUser;
import com.sale.salesystem.mapper.UserMapper;
import com.sale.salesystem.service.UserService;
import com.sale.salesystem.dto.UserLoginDTO;
import com.sale.salesystem.dto.UserRegisterDTO;
import com.sale.salesystem.vo.UserVO;
import com.sale.salesystem.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    private final UserMapper userMapper;

    @Override
    public UserVO login(UserLoginDTO loginDTO) {
        // 查询用户
        SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, loginDTO.getUsername()));

        if (user == null) {
            throw new BusinessException("用户不存在");
        }

        // 验证密码
        String pwd = loginDTO.getPassword();
        if (!user.getPassword().equals(pwd)) {
            throw new BusinessException("密码错误");
        }

        // 返回用户信息
        return convertToVO(user);
    }

    @Override
    public UserVO register(UserRegisterDTO registerDTO) {
        // 验证密码
        if (!registerDTO.getPassword().equals(registerDTO.getConfirmPassword())) {
            throw new BusinessException("两次密码不一致");
        }

        // 检查用户名是否存在
        if (userMapper.selectCount(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, registerDTO.getUsername())) > 0) {
            throw new BusinessException("用户名已存在");
        }

        // 创建用户
        SysUser user = new SysUser();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(registerDTO.getPassword());
        user.setRole("VISITOR");

        userMapper.insert(user);

        return convertToVO(user);
    }

    @Override
    public UserVO getCurrentUser() {
        // TODO: 从当前会话获取用户信息
        return null;
    }

    @Override
    public List<UserVO> searchByUsername(String username) {
        List<SysUser> users = userMapper.selectList(new LambdaQueryWrapper<SysUser>()
                .like(SysUser::getUsername, username)
                .last("limit 10"));  // 限制返回数量
                
        return users.stream()
                .map(user -> {
                    UserVO vo = new UserVO();
                    vo.setId(user.getId());
                    vo.setUsername(user.getUsername());
                    vo.setRole(user.getRole());
                    return vo;
                })
                .collect(Collectors.toList());
    }

    private UserVO convertToVO(SysUser user) {
        UserVO vo = new UserVO();
        vo.setId(user.getId());
        vo.setUsername(user.getUsername());
        vo.setRole(user.getRole());
        vo.setToken("mock-token");
        return vo;
    }
}